/*************************************************************************
	> File Name: toward_M0.c
	> Author: 
	> Mail: 
	> Created Time: 2018年08月28日 星期二 15时08分09秒
 ************************************************************************/

#include <toward_M0.h>

/*
 * 函数名:toward_M0
 * 功能:向M0办发送数据
 * 参数:int fifo_fd:管道打开的标识符
 * 返回值:发送送成功返回OK，失败返回ERR
 */
int toward_M0(int fifo_fd)
{
	
	int ret = 0;
	env_dev_rfid_card *edrc = (env_dev_rfid_card *)malloc(sizeof(env_dev_rfid_card));
	memset(edrc, 0, sizeof(env_dev_rfid_card));
	char *buf = (char *)malloc(200);
	memset(buf, 0, 200);
	ret = read(fifo_fd, buf, sizeof(fifo));
	
	if (0 > ret)
	{
		free(edrc);
		free(buf);
		return ERR;
	}
	if (0 == memcmp(buf, DEVI, strlen(DEVI)))	//设备控制信息
	{
		edrc->rfid_card = 0;
		memcpy(&edrc->dev, buf + strlen(DEVI), sizeof(dev));
		send_M0(*edrc);	
	}
	else if (0 == memcmp(buf, MJKH, strlen(MJKH)))	//刷卡信息反馈
	{
		//printf("+-+-+-+-+-+-+-+-+-+-+:rf_id=%d\n",*(int *)(buf + strlen(MJKH)));
		//printf("send_M0########## buf = %s\n", buf);	
		edrc->rfid_card = *(int *)(buf + strlen(MJKH));
		send_M0(*edrc);
		//printf("send_M0##########xiang M0 ban fa song xin xi : %d\n", edrc->rfid_card);
	}
	else if (0 == memcmp(buf, FFCR, strlen(FFCR)))	//刷卡信息反馈
	{
		//printf("+-+-+-+-+-+-+-+-+-+-+:rf_id=%d\n",*(int *)(buf + strlen(MJKH)));
		//printf("send_M0########## buf = %s\n", buf);	
		edrc->rfid_card = *(int *)(buf + strlen(FFCR));
		send_M0(*edrc);
		//printf("send_M0##########xiang M0 ban fa song xin xi : %d\n", edrc->rfid_card);
	}
	free(edrc);
	free(buf);
	
	return OK;
}

